<template>
	<view class="body">
		<xk-header :headerbg="true" :back="true" title="预约看房"></xk-header>

		<view class="tabs-bar" scroll-x="true">
			<view v-for="(item, index) of tabs" :key="index" :class="tabIndex === index ? 'item item-a' : 'item'" @click="clickTab(index)" >{{item.title}}</view>
		</view>
		<view class="list-info">
			<swiper @change="listInfoChange" :current="tabIndex"
				:style=" {height: (tabIndex==0 ? scrollViewHeightBottom : scrollViewHeight) + 'px'}">
				<swiper-item v-for="(tabItem, tabIndex) of tabs" :key="tabIndex">
					<scroll-view class="data-list" scroll-y="true"
						refresher-enabled="true" :refresher-triggered="tabs[tabIndex].triggered" :refresher-threshold="100"
						@scrolltolower="onScrollToLower()"  @refresherrefresh="onRefresh()">
						<checkbox-group @change="checkboxChange">
							<view class="data-item" v-if="dataList[tabIndex] && dataList[tabIndex].length > 0"
								v-for="(item1, index1) of dataList[tabIndex]" :key="index1">
								<view class="item-title">
									<view class="title">单号: {{item1.apply_no || ""}}</view>
									<view :class="item1.jd_status==='APPLYING' ? 'status-other status' : 'status-other'">{{$dicUtil.PiLookingRoomJdStatus(item1.jd_status)}}</view>
								</view>
								<view  v-for="(item, index) of item1.pi_looking_property_dets" :key="index">
									<view class="item-top">
										<checkbox :value="index1+''" :checked="selectIndexList.indexOf(index1+'') !== -1"
											v-if="item1.jd_status === 'APPLYING'"/>
										<view class="title">物业编号: {{item.property_code || ""}}</view>
									</view>
									<view class="item-middel">
										<image v-if="item.property_image" mode="aspectFill" class="img" :src="item.property_image | formatImgPath('260', '195')"/>
										<image v-else mode="aspectFill" class="img" :src="(imgUrl+'/wx/default_img.jpg')"/>
										<view class="info">
											<view>
												<view class="name">{{item.tag_name || "-"}}</view>
												<view class="desc">实用{{item.real_area || ''}}㎡/总面积{{item.floor_area}}㎡</view>
											</view>
											<view>
												<view v-if="item.pricing_amount" class="amount"><text></text>{{(item.pricing_amount * item.floor_area || "") | formatMoneyNo00}} <text>元/月</text></view>
												<view v-else class="amount">月租面议</view>
												<view v-if="item.pricing_amount" class="desc">{{(item.pricing_amount || "") | formatMoneyNo00}}元/㎡</view>
											</view>
										</view>
									</view>
									<view class="location-view">
										<image mode="aspectFill" class="dingwei" :src="(imgUrl+'/wx/dingwei.png')"/>
										<view>
											<view class="address">{{item.building || "-"}}/{{item.storey || "-"}}</view>
										</view>
									</view>

									<view class="date-view" v-if="index+1 == item1.pi_looking_property_dets.length &&  item1.jd_status !== 'APPLYING'">
										<view class="date1">看房时间: {{item1.start_time}}</view>
										<view class="date2">接待时间: {{item1.jd_status === 'APPLYED' ? "受理后展示" : (item1.accept_time || "")}}</view>
									</view>
								</view>
								<view class="btn-view" v-if="item1.jd_status === 'APPLYED'">
									<view class="common-btn btn-sty2 btn btnNoBg" @click="onCancel(item1)">取消预约</view>
									<!-- <view class="common-btn btn-sty2 btn" @click="onChangeDate(item1)">变更时间</view> -->
								</view>

								<view class="btn-view" v-if="item1.jd_status === 'APPLYING'">
									<view class="common-btn btn-sty2 btn" @click="onCancel(item1)">取消</view>
								</view>
							</view>
						</checkbox-group>

						<view class="empty-view" v-if="!dataList[tabIndex] || dataList[tabIndex] <= 0">
							<image class="empty-icon" :src="(imgUrl+'/wx/empty_data_icon.png')"></image>
							<view>哎呀，该界面暂无内容~</view>
						</view>
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>

		<shopping-cart ref="shoppingCart" class="shopping-cart" :style="{top: cartTop + 'px'}"/>

		<checkbox-group @change="allCheckChange">
			<view class="bottom-view" v-if="tabIndex===0 && selectIndexList.length > 0">
				<view>
					<view class="left-view">
						<checkbox value="all"/>
						<view>全选共</view>
						<view class="number">{{selectIndexList.length}}</view>
						<view>套</view>
					</view>
					<view class="common-btn btn" @click="onAppt()">立即预约</view>
				</view>
			</view>
			</checkbox-group>
		<ApptTimePicker ref="apptTimePicker" @change="startTimeChange" title="查看物业时间段"/>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imgUrl:this.$config.BASE_imgUrl,
				cartTop: 0,
				scrollViewHeightBottom: 0,
				scrollViewHeight: 0,
				tabIndex: 0,
				tabs: [{
					title: "待看房",
					start: 0,
					isLoad: false,	// 数据加载标识
					isReload: false,	// 重新载入标识
					triggered: false
				}, {
					title: "已看房",
					start: 0,
					isLoad: false,	// 数据加载标识
					isReload: false,	// 重新载入标识
					triggered: false
				}],
				dataList: [[], []],
				selectIndexList: [],
				curItem: {},
			};
		},
		onLoad(){
			this.initInfo()
		},
		onShow() {
			this.updateHeight()
		},
		methods:{
			initInfo(){
				// 设置所有加载标识
				for (let index in this.tabs) {
					this.tabs[index].isLoad = true
				}
				this.queryList()
			},
			updateHeight() {
				this.$util.getHeightWithTopEl(".tabs-bar").then(height => {
					this.scrollViewHeight = height - 10;

					if (this.selectIndexList.length > 0) {
						this.$util.getElRect(".bottom-view").then(rect => {
							this.scrollViewHeightBottom = height - rect.height - 10;

							this.cartTop = rect.top - 100
						})
					} else {
						this.scrollViewHeightBottom = this.scrollViewHeight

						uni.getSystemInfo({
							success:(res)=> {
								this.cartTop = res.screenHeight - 100;
								console.warn("carTop: ", this.cartTop)
							}
						});
					}
				});


			},
			clickTab(index){
				this.tabIndex = index
			},
			queryList() {
				// 判断是否重新加载，isReload为true时，清空对应的列表数据
				for (let index in this.tabs) {
					let tab = this.tabs[index]
					if (tab.isReload) {	// 重新加载数据
						this.dataList[index].splice(0, this.dataList[index].length);
						this.tabs[index].isReload = false
						this.tabs[index].start = 0
						this.tabs[index].isLoad = true
					}
				}

				this.$api.piLookingRoom_getPiLookingRoomList({
					start: this.tabs[this.tabIndex].start
				}).then(res=>{
					console.warn("查询预约看房列表: ", res)

					// 未完成列表
					if (this.tabs[0].isLoad) {
						for (let item of res.unDoneLookingRoomList) {
							this.dataList[0].push(item)
						}
						this.tabs[0].start += res.unDoneLookingRoomList.length
					}
					// 已完成列表
					if (this.tabs[1].isLoad) {
						for (let item of res.completeLookingRoomList) {	// 已看房
							this.dataList[1].push(item)
						}
						this.tabs[1].start += res.completeLookingRoomList.length
					}

					// 设置所有加载标识
					for (let index in this.tabs) {
						this.tabs[index].isLoad = false

						setTimeout(() => {
							this.tabs[index].triggered = false
						}, 1000)
					}

				})
			},
			listInfoChange(e){
				this.tabIndex = parseInt(e.target.current)
			},
			goDetail(item) {
				uni.navigateTo({
					url: "/pages/estate/estate-info"
				})
			},
			allCheckChange(e) {
				if (e.detail.value.length > 0) {	// 全选
					this.selectIndexList = [];
					for (let index in this.dataList[this.tabIndex]) {
						let item = this.dataList[this.tabIndex][index]
						if(item.jd_status == 'APPLYING'){
							this.selectIndexList.push(index)
						}
					}
				} else {
					this.selectIndexList = [];
				}

				this.updateHeight()
			},
			checkboxChange(e) {
				console.warn("checkboxChange: ", e.detail.value)
				this.selectIndexList = e.detail.value

				this.updateHeight()
			},
			onCancel(item) {
				this.$util.confirm({
					content: "确定要取消当前物业预约?",
					confirmText: "取消预约"
				}).then(isConfirm=>{
					if (isConfirm) {
						this.$util.post({
							url: "wx/piLookingRoom/cancel",
							data: {
								look_id: item.look_id
							}
						}).then((res)=>{
							this.$util.toast("预约取消成功")
							this.dataList[this.tabIndex].splice(this.dataList[this.tabIndex].indexOf(item), 1)
						});
					}
				})
			},
			onChangeDate(item) {
				this.curItem = item
				this.$refs.apptTimePicker.show()
			},
			startTimeChange(value) {
				let assetsPropertyIds = [];

				for (let item of this.curItem.pi_looking_property_dets) {
					assetsPropertyIds.push(item.property_id)
				}
				this.$util.post({
					url: "piLookingRoom/applyUpdate",
					data: {
						look_id: this.curItem.look_id,
						leasePurpose: this.curItem.lease_purpose,
						property_remark: this.curItem.property_remark,
						isSubmit: true,
						startTime: value,
						assetsPropertyIds: assetsPropertyIds
					}
				}).then((res)=>{
					this.$util.toast("变更时间成功")
					let index = this.dataList[this.tabIndex].indexOf(this.curItem)
					this.dataList[this.tabIndex][index]["start_time"] = value
				});
			},
			onAppt() {
				if (this.selectIndexList.length <= 0) {
					this.$util.toast("请先选择要预约的物业")
					return;
				}
				let dataListTemp = []
				for (let index of this.selectIndexList) {
					dataListTemp.push(this.dataList[this.tabIndex][index])
				}

				let eventName = "make-appt";
				this.$EventBus.$off(eventName)
				this.$EventBus.$on(eventName, item => {
					this.$EventBus.$off(eventName)

					this.selectIndexList = [];
					// 设置数据加载标识为重新加载
					this.tabs[0].isReload = true
					this.queryList()
				})

				uni.navigateTo({
					url: "/pages/estate/make-appt-house?isListIn=1&dataList="+JSON.stringify(dataListTemp)+"&eventName="+eventName
				})
			},
			onScrollToLower(e) {
				if (this.tabs[this.tabIndex].isLoad) return
				this.tabs[this.tabIndex].isLoad = true
				this.queryList()
			},
			onRefresh() {
				console.warn("sdfsdfsdf: ", this.tabs[this.tabIndex].isLoad)
				if (this.tabs[this.tabIndex].isLoad) return
				this.tabs[this.tabIndex].isReload = true
				this.tabs[this.tabIndex].triggered = true
				this.queryList()
			},
		}
	}
</script>

<style lang="scss">

.tabs-bar{
	display: flex;
	align-items: center;
	flex-direction: row;
	justify-content: space-around;
	padding: 20rpx 30rpx 0rpx 30rpx;
	width: 750rpx;
	background-color: #FFFFFF;
	white-space: nowrap;
	.item{
		display: inline-block;
		height: 65rpx;
		align-items: center;
		justify-content: center;
		font-size: 32rpx;
		margin-right: 60rpx;
		color: #333333;
		border-bottom: 6rpx solid rgba(#2C72FF, 0);
	}
	.item-a {
		font-weight: bolder;
		border-bottom: 6rpx solid #2C72FF;
	}
}

.list-info {
	.data-list{
		border-radius: 10rpx;
		height: 100%;
		.data-item {
			display: flex;
			flex-direction: column;
			margin-bottom: 30rpx;
			background-color: #FFFFFF;
			border-radius: 10rpx;
			margin: 20rpx 30rpx 0rpx 30rpx;
			padding: 20rpx;
			color: #333;
			font-size: 28rpx;
			white-space: normal;
			.item-title {
				display: flex;
				align-items: center;
				font-size: 28rpx;

				.title {
					flex: 1;
				}

				.status-other {
					padding: 5rpx 15rpx;
					font-size: 24rpx;
					color: #2C72FF ;
					background-color: rgba($color: #A5C2FF, $alpha: 0.16);
					border-radius: 5rpx;
				}

				.status {
					color: #FF6400;
					background-color: rgba($color: #FFCDA5, $alpha: 0.16);
				}
			}
			.item-top {
				display: flex;
				align-items: center;
				margin-top: 30rpx;

				>checkbox {
					margin-right: 5rpx;
				}


				.title {
					flex: 1;
				}
			}

			.item-middel {
				display: flex;
				margin-top: 10rpx;

				.img{
					width: 200rpx;
					height: 150rpx;
					border-radius: 10rpx;
					margin-right: 20rpx;
				}
				.info{
					flex: 1;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					.name{
						font-size: 28rpx;
						font-weight: 600;
						color: #333333;
					}
					.desc{
						font-size: 24rpx;
						color: #999999;
						font-weight: 400;
					}
					.amount{
						font-size: 36rpx;
						color: #FF3737;
						font-weight: 700;
						text{
							font-size: 24rpx;
							color: #FF3737;
							font-weight: 600;
						}
					}
				}
			}
			.location-view {
				display: flex;
				margin-top: 30rpx;
				padding-bottom: 20rpx;
				align-items: flex-start;
				font-size: 24rpx;
				border-bottom: 1rpx #eee solid;

				.dingwei {
					width: 30rpx;
					height: 30rpx;
					margin-top: 5rpx;
				}
				.address {
					color: #333;
					margin-left: 10rpx;
				}
				.date1 {
					color: #999;
					margin: 10rpx 0rpx;
				}
				.date2 {
					color: #2C72FF;
				}
			}

			.data-sub-item {
				margin-top: 20rpx;
				padding: 20rpx;
				background-color: #F8F8F8;
			}
			.date-view {
				font-size: 28rpx;
				margin-top: 30rpx;
				margin-bottom: 30rpx;
				:first-child {
					color: #999;
				}
				:last-child {
					color: #2C72FF;
					margin-top: 20rpx;
				}
			}
			.btn-view {
				display: flex;
				align-items: flex-end;
				justify-content:flex-end;

				.btn {
					font-size: 28rpx;
					width: 175rpx;
					height: 60rpx;
					margin: 0rpx 0rpx 10rpx 0rpx;
				}
				.btnNoBg {
					background-color: #FFFFFF;
					border: 1rpx #999999 solid;
					margin-right: 20rpx;
				}

			}
		}
	}
}

.bottom-view {
	position: fixed;
	width: 750rpx;
	bottom: var(--window-bottom);

	>view {
		display: flex;
		flex: 1;
		align-items: center;
		background-color: #FFFFFF;
		padding: 35rpx 30rpx 35rpx 30rpx;

		.left-view {
			display: flex;
			align-items: center;
			flex: 1;
			font-size: 28rpx;
			color: #333;

			.number {
				color: #FF3737;
				margin: 0 10rpx;
				font-weight: bold;
			}
		}
		.btn {
			width: 280rpx;
			height: 80rpx;
			padding: 0rpx;
			margin: 0rpx;
		}
	}
}


</style>
